package com.mystnihon.freeremote.db;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.mystnihon.freeremote.db.table.ChannelTable;
import com.mystnihon.freeremote.db.table.ProgramTable;
import com.mystnihon.freeremote.db.table.SeedboxTable;

public class DBProvider extends ContentProvider {

    public static final UriMatcher sUriMatcher;
    public static final String AUTHORITY;

    private static final int SEEDBOX = 0x0100;
    private static final int CHANNEL = 0x0200;
    private static final int PROGRAM = 0x0300;

    public static final Uri RAW_QUERY_URI;
    private static final int RAW_QUERY_ID = 0x9000;
    private static final String RAW_QUERY_ACTION = "rawQuery";

    static {
        AUTHORITY = "com.mystnihon.freeremote.db.authorithy";
        RAW_QUERY_URI = Uri.parse("content://" + DBProvider.AUTHORITY + "/" + RAW_QUERY_ACTION);

        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(AUTHORITY, SeedboxTable.TABLE_NAME, SEEDBOX);
        sUriMatcher.addURI(AUTHORITY, ChannelTable.TABLE_NAME, CHANNEL);
        sUriMatcher.addURI(AUTHORITY, ProgramTable.TABLE_NAME, PROGRAM);
        sUriMatcher.addURI(AUTHORITY, RAW_QUERY_ACTION, RAW_QUERY_ID);
    }

    private SQLiteDatabase mDatabase;

    public synchronized SQLiteDatabase getDatabase() {
        // Always return the cached database, if we've got one
        if (mDatabase != null) {
            return mDatabase;
        }

        final DBHelper helper = new DBHelper(getContext());
        mDatabase = helper.getWritableDatabase();
        return mDatabase;
    }

    static SQLiteDatabase getReadableDatabase(final Context context) {
        final DBHelper helper = new DBHelper(context);
        return helper.getReadableDatabase();
    }

    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {
        final Context ctx = getContext();
        final SQLiteDatabase db = getDatabase();
        int count = 0;
        switch (sUriMatcher.match(uri)) {
            case SEEDBOX:
                count = db.delete(SeedboxTable.TABLE_NAME, where, whereArgs);
                break;
            case CHANNEL:
                count = db.delete(ChannelTable.TABLE_NAME, where, whereArgs);
                break;
            case PROGRAM:
                count = db.delete(ProgramTable.TABLE_NAME, where, whereArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
        ctx.getContentResolver().notifyChange(uri, null);
        return count;
    }

    @Override
    public String getType(Uri uri) {
        switch (sUriMatcher.match(uri)) {
            case SEEDBOX:
                return SeedboxTable.CONTENT_TYPE;
            case CHANNEL:
                return ChannelTable.CONTENT_TYPE;
            case PROGRAM:
                return ProgramTable.CONTENT_TYPE;
            case RAW_QUERY_ID:
                return "vnd.android.cursor.item/vnd.freeremote." + RAW_QUERY_ACTION;
            default:
                return null;
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        final SQLiteDatabase db = getDatabase();
        long rowId = 0;
        Uri noteUri = null;
        if (values != null) {
            switch (sUriMatcher.match(uri)) {
                case SEEDBOX:
                    rowId = db.insert(SeedboxTable.TABLE_NAME, null, values);
                    noteUri = ContentUris.withAppendedId(SeedboxTable.CONTENT_URI, rowId);
                    break;
                case CHANNEL:
                    rowId = db.insert(ChannelTable.TABLE_NAME, null, values);
                    noteUri = ContentUris.withAppendedId(ChannelTable.CONTENT_URI, rowId);
                    break;
                case PROGRAM:
                    rowId = db.insert(ProgramTable.TABLE_NAME, null, values);
                    noteUri = ContentUris.withAppendedId(ProgramTable.CONTENT_URI, rowId);
                    break;
                default:
                    throw new IllegalArgumentException("Unknown URI " + uri);
            }
        }
        // if (rowId > 0) {
        getContext().getContentResolver().notifyChange(noteUri, null);
        return noteUri;
        // }
        // else {
        // throw new SQLException("Failed to insert row into " + uri);
        // }
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int result = 0;
        final SQLiteDatabase db = getDatabase();
        if (values != null) {
            switch (sUriMatcher.match(uri)) {
                case SEEDBOX:
                    result = db.update(SeedboxTable.TABLE_NAME, values, selection, selectionArgs);
                    break;
                case CHANNEL:
                    result = db.update(ChannelTable.TABLE_NAME, values, selection, selectionArgs);
                    break;
                case PROGRAM:
                    result = db.update(ProgramTable.TABLE_NAME, values, selection, selectionArgs);
                    break;
                default:
                    throw new IllegalArgumentException("Unknown URI " + uri);
            }
            getContext().getContentResolver().notifyChange(uri, null);
        }
        return result;
    }

    @Override
    public Cursor query(final Uri uri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder) {
        Cursor c = null;
        final SQLiteDatabase db = getDatabase();
        switch (sUriMatcher.match(uri)) {
            case SEEDBOX:
                c = db.query(SeedboxTable.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case CHANNEL:
                c = db.query(ChannelTable.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case PROGRAM:
                c = db.query(ProgramTable.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case RAW_QUERY_ID:
            	System.out.println("Raw Query !! "+selection);
                c = db.rawQuery(selection, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
        if (c != null && !isTemporary()) {
            c.setNotificationUri(getContext().getContentResolver(), uri);
        }
        return c;
    }

    @Override
    public boolean onCreate() {
        getDatabase();
        return true;
    }

}
